package ar.org.fiel.tarifasocial.om.base;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;

import ar.org.fiel.tarifasocial.om.model.*;

import ar.org.fiel.tarifasocial.om.peer.*;

                        

/**
 * This class was autogenerated by Torque on:
 *
 * [Mon Jun 18 20:18:58 ART 2007]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to DocumentItem
 */
public abstract class BaseDocumentItem extends BaseObject
{
    /** The Peer class */
    private static final DocumentItemPeer peer =
        new DocumentItemPeer();

        
    /** The value for the id field */
    private Integer id;
      
    /** The value for the sectionId field */
    private Integer sectionId;
      
    /** The value for the documentId field */
    private Integer documentId;
      
    /** The value for the order field */
    private Integer order;
  
            
    /**
     * Get the Id
     *
     * @return Integer
     */
    public Integer getId()
    {
        return id;
    }

                        
    /**
     * Set the value of Id
     *
     * @param v new value
     */
    public void setId(Integer v) 
    {
    
                  if (!ObjectUtils.equals(this.id, v))
              {
            this.id = v;
            setModified(true);
        }
    
          
              }
          
    /**
     * Get the SectionId
     *
     * @return Integer
     */
    public Integer getSectionId()
    {
        return sectionId;
    }

                              
    /**
     * Set the value of SectionId
     *
     * @param v new value
     */
    public void setSectionId(Integer v) throws TorqueException
    {
    
                  if (!ObjectUtils.equals(this.sectionId, v))
              {
            this.sectionId = v;
            setModified(true);
        }
    
                                  
                if (aSection != null && !ObjectUtils.equals(aSection.getId(), v))
                {
            aSection = null;
        }
      
              }
          
    /**
     * Get the DocumentId
     *
     * @return Integer
     */
    public Integer getDocumentId()
    {
        return documentId;
    }

                              
    /**
     * Set the value of DocumentId
     *
     * @param v new value
     */
    public void setDocumentId(Integer v) throws TorqueException
    {
    
                  if (!ObjectUtils.equals(this.documentId, v))
              {
            this.documentId = v;
            setModified(true);
        }
    
                                  
                if (aDocument != null && !ObjectUtils.equals(aDocument.getId(), v))
                {
            aDocument = null;
        }
      
              }
          
    /**
     * Get the Order
     *
     * @return Integer
     */
    public Integer getOrder()
    {
        return order;
    }

                        
    /**
     * Set the value of Order
     *
     * @param v new value
     */
    public void setOrder(Integer v) 
    {
    
                  if (!ObjectUtils.equals(this.order, v))
              {
            this.order = v;
            setModified(true);
        }
    
          
              }
  
      
        
                  
    
        private Section aSection;

    /**
     * Declares an association between this object and a Section object
     *
     * @param v Section
     * @throws TorqueException
     */
    public void setSection(Section v) throws TorqueException
    {
            if (v == null)
        {
                  setSectionId((Integer) null);
              }
        else
        {
            setSectionId(v.getId());
        }
            aSection = v;
    }

                        
    /**
     * Returns the associated Section object.
           * If it was not retrieved before, the object is retrieved from
     * the database
           *
     * @return the associated Section object
           * @throws TorqueException
           */
    public Section getSection()
              throws TorqueException
          {
              if (aSection == null && (!ObjectUtils.equals(this.sectionId, null)))
        {
                              aSection = SectionPeer.retrieveByPK(SimpleKey.keyFor(this.sectionId));
                  
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               Section obj = SectionPeer.retrieveByPK(this.sectionId);
               obj.add${pCollName}(this);
            */
        }
              return aSection;
    }

    /**
     * Return the associated Section object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Section object
     * @throws TorqueException
     */
    public Section getSection(Connection connection)
        throws TorqueException
    {
        if (aSection == null && (!ObjectUtils.equals(this.sectionId, null)))
        {
                          aSection = SectionPeer.retrieveByPK(SimpleKey.keyFor(this.sectionId), connection);
              
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               Section obj = SectionPeer.retrieveByPK(this.sectionId, connection);
               obj.add${pCollName}(this);
            */
        }
        return aSection;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
         */
    public void setSectionKey(ObjectKey key) throws TorqueException
    {
      
                        setSectionId(new Integer(((NumberKey) key).intValue()));
                  }
    
        
                  
    
        private Document aDocument;

    /**
     * Declares an association between this object and a Document object
     *
     * @param v Document
     * @throws TorqueException
     */
    public void setDocument(Document v) throws TorqueException
    {
            if (v == null)
        {
                  setDocumentId((Integer) null);
              }
        else
        {
            setDocumentId(v.getId());
        }
            aDocument = v;
    }

                        
    /**
     * Returns the associated Document object.
           * If it was not retrieved before, the object is retrieved from
     * the database
           *
     * @return the associated Document object
           * @throws TorqueException
           */
    public Document getDocument()
              throws TorqueException
          {
              if (aDocument == null && (!ObjectUtils.equals(this.documentId, null)))
        {
                              aDocument = DocumentPeer.retrieveByPK(SimpleKey.keyFor(this.documentId));
                  
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               Document obj = DocumentPeer.retrieveByPK(this.documentId);
               obj.add${pCollName}(this);
            */
        }
              return aDocument;
    }

    /**
     * Return the associated Document object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Document object
     * @throws TorqueException
     */
    public Document getDocument(Connection connection)
        throws TorqueException
    {
        if (aDocument == null && (!ObjectUtils.equals(this.documentId, null)))
        {
                          aDocument = DocumentPeer.retrieveByPK(SimpleKey.keyFor(this.documentId), connection);
              
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               Document obj = DocumentPeer.retrieveByPK(this.documentId, connection);
               obj.add${pCollName}(this);
            */
        }
        return aDocument;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
         */
    public void setDocumentKey(ObjectKey key) throws TorqueException
    {
      
                        setDocumentId(new Integer(((NumberKey) key).intValue()));
                  }
       
                
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
          save(DocumentItemPeer.getMapBuilder()
                .getDatabaseMap().getName());
      }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
       * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
       *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
          try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
      }

      /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
      /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
          if (!alreadyInSave)
        {
            alreadyInSave = true;


  
            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    DocumentItemPeer.doInsert((DocumentItem) this, con);
                    setNew(false);
                }
                else
                {
                    DocumentItemPeer.doUpdate((DocumentItem) this, con);
                }
                }

                      alreadyInSave = false;
        }
      }

                  
      /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param key id ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        
    {
            setId(new Integer(((NumberKey) key).intValue()));
        }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) 
    {
            setId(new Integer(key));
        }

  
    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
          return SimpleKey.keyFor(getId());
      }
 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
       * It then fills all the association collections and sets the
     * related objects to isNew=true.
       */
      public DocumentItem copy() throws TorqueException
    {
        return copyInto(new DocumentItem());
    }
  
    protected DocumentItem copyInto(DocumentItem copyObj) throws TorqueException
    {
          copyObj.setId(id);
          copyObj.setSectionId(sectionId);
          copyObj.setDocumentId(documentId);
          copyObj.setOrder(order);
  
                    copyObj.setId((Integer)null);
                              
                return copyObj;
    }

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public DocumentItemPeer getPeer()
    {
        return peer;
    }


    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("DocumentItem:\n");
        str.append("Id = ")
               .append(getId())
             .append("\n");
        str.append("SectionId = ")
               .append(getSectionId())
             .append("\n");
        str.append("DocumentId = ")
               .append(getDocumentId())
             .append("\n");
        str.append("Order = ")
               .append(getOrder())
             .append("\n");
        return(str.toString());
    }
}
